<?php

namespace app\xwam\model;

/**
 * Description of OrderModel
 *
 * @since 1.0 <2016-5-26> SoChishun <14507247@qq.com> Added.
 */
class OrderModel extends AppbaseModel {

    protected $table = 't_porg_order';

    public function addOrder($data) {
        $validator = array(
            array('user_name', 'require', '用户名无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('express_no', 'require', '快递单号无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
        );
        if (!$this->validate($validator)->create($data)) {
            return $this->returnMsg(false, $this->getError());
        }
        // 写入订单
        $serial_no = xsfn_generate_serial();
        $delivery_fee = I('delivery_fee');
        $express_no = I('express_no');
        $data_order = array(
            'serial_no' => $serial_no,
            'consignee_name' => '',
            'consignee_telphone' => '',
            'consignee_address' => '',
            'summary' => '',
            'express_no'=>$express_no,
            'item_count' => 0,
            'product_amount' => 0,
            'discount_amount' => 0,
            'order_amount' => 0,
            'delivery_fee' => $delivery_fee,
            'buyer_name' => I('user_name'),
            'buyer_remark' => I('buyer_remark'),
        );
        $order_id = $this->add($data_order);
        if (false === $order_id) {
            return $this->returnMsg(false);
        }
        // 获取商品数据
        $product_ids = I('product_id');
        $product_names=I('product_name');
        $product_marques = I('product_marque');
        $numbers = I('number');
        $prices = I('price');
        $discount_amounts = I('discount_amount');
        $subtotals = I('subtotal');
        $i = 0;
        $update_order = array('item_count' => 0, 'product_amount' => 0, 'discount_amount' => 0, 'order_amount' => $delivery_fee);
        foreach ($product_ids as $product_id) {
            if (!$product_id) {
                $i++;
                continue;
            }
            $data_product = array(
                'order_id' => $order_id,
                'order_serial_no' => $serial_no,
                'product_id' => $product_id,
                'product_name' => $product_names[$i],
                'product_marque' => $product_marques[$i],
                'number' => $numbers[$i],
                'price' => $prices[$i],
                'discount_amount' => $discount_amounts[$i],
                'subtotal' => $subtotals[$i],
            );
            $update_order['item_count'] = $update_order['item_count'] + $data_product['number'];
            $update_order['product_amount'] = $update_order['product_amount'] + $data_product['subtotal'] + $data_product['discount_amount'];
            $update_order['discount_amount'] = $update_order['discount_amount'] + $data_product['discount_amount'];
            $update_order['order_amount'] = $update_order['order_amount'] + $data_product['subtotal'];
            $this->table('t_porg_order_product_list')->add($data_product);
            $i++;
        }
        $this->where(array('id' => $order_id))->setField($update_order);
        return $this->returnMsg(true);
    }

}
